package com.wenge.datagroup.storage.dataprocess;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：WeiPengCheng
 * @Date：2022-07-27 10:55
 * @desc:多语言分词
 **/
public class MultilingualSpiltWords {
    private static Logger logger = LoggerFactory.getLogger(MultilingualSpiltWords.class);
    private static String url;

    public static void setUrl(String url) {
        MultilingualSpiltWords.url = url;
    }

    public static void main(String[] args) {
        PropertyConfigurator.configure("config/log4j.properties");
        String content =
                "{\"text\":\"一直用这款图鉴，简洁明了，各种功能都有，用了5年了\",\"allow_pos\":0}\n";
        analysis(content);
    }

    public static List<String> analysis(String par) {
        long start = System.currentTimeMillis();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("text", par);
        jsonObject.put("allow_pos", 0);

        OkHttpClient httpClient = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/json");
        MultipartBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("id", "1")
                .addFormDataPart("content", jsonObject.toJSONString())
                .build();

        Request request = new Request.Builder()
                .url(url)
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();

        // 4 发起请求获取响应值
        try {
            Response response = httpClient.newCall(request).execute();
            if (response.code() == 200) {
                logger.info("多语种分词成功!!");
                String data = response.body().string();
                System.out.println(data);
                if (JSON.parseObject(data).getString("code").equals("200")) {
                    JSONArray jsonArray = JSON
                            .parseObject(data)
                            .getJSONObject("data")
                            .getJSONArray("result");

                    List<String> list = new ArrayList<>();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        String s = jsonArray.getString(i).replaceAll("[\\pP\\p{Punct}]", "|");
                        if (s.equals("|")) {
                            continue;
                        } else {
                            list.add(s);
                        }
                    }
                    logger.info("多语言分词结果:" + list);
                    return list;
                }
            } else {
                logger.info("多语言分词接口请求异常! code:{}", response.code());
            }
        } catch (IOException e) {
            logger.error("多语言分词接口请求异常!");
            e.printStackTrace();
        } finally {
            logger.info("多语言分词接口处理耗时： " + (System.currentTimeMillis() - start) + " ms");
        }

        return new ArrayList<String>();
    }

}
